<?php
if(!isset($in_pmt)) header("Location:index.php");

$day_of_week = date('D', mktime (0,0,0,$date->curr_month, 1, $date->curr_year));
$days_in_month = cal_days_in_month(0,$date->curr_month,$date->curr_year);

switch($day_of_week){
	case "Mon": $blank = 0; break;
	case "Tue": $blank = 1; break;
	case "Wed": $blank = 2; break;
	case "Thu": $blank = 3; break;
	case "Fri": $blank = 4; break;
	case "Sat": $blank = 5; break;
	case "Sun": $blank = 6; break;

}

$msg->write("<table id=\"month\">\n");
$msg->write("<tr>\n<th colspan=60>\n<a href=\"index.php?p=month&amp;date={$date->prev_year}-{$date->prev_month}-01\">&lt;&lt;{$date->prev_month_title} {$date->prev_year}&lt;&lt;</a> 
		| {$date->curr_month_title} {$date->curr_year} | 
		<a href=\"index.php?p=month&amp;date={$date->next_year}-{$date->next_month}-01\">&gt;&gt;{$date->next_month_title} {$date->next_year}&gt;&gt;</a>\n</th>\n</tr>\n");
$msg->write("<tr style=\"font-weight: bold\">\n<td width=62>По</td>\n<td width=62>Вт</td>\n<td width=62>Ср</td>\n<td width=62>Че</td>\n<td width=62>Пе</td>\n<td width=62>Съ</td>\n<td width=62>Не</td>\n</tr>\n");
$day_count = 1;
$msg->write("<tr>\n");
while ($blank>0){
	$msg->write("<td></td>\n");
	$blank = $blank-1;
	$day_count++;
}
$mont_data =  array();
//$month_data[$days_in_month-1];
for($day_num =1;$day_num<=$days_in_month;$day_num++){
	if($day_num <10){
		$day_num_link = "0".$day_num;
	} else {
		$day_num_link = $day_num;
	}
	
	if($day_num>(int)$date->today_day && $year>=$date->today_year && $date->curr_month >=$date->today_month)
	{
			$msg->write("<td class=\"day\"> $day_num");
	} 
	elseif($day_num==(int)$date->today_day && $date->curr_year==$date->today_year && $date->curr_month ==$date->today_month)
	{
		$msg->write("<td class=\"day today\"><a href=\"index.php?p=day&amp;date={$date->curr_year}-{$date->curr_month}-{$day_num_link}\">$day_num</a>");
	} 
	else 
	{
		$msg->write(" <td><a href=\"index.php?p=day&amp;date={$date->curr_year}-{$date->curr_month}-$day_num_link\">$day_num</a>");
	}
	$result = $db->query("select * from balances where date = '{$date->curr_year}-{$date->curr_month}-$day_num_link'");
	
	if($db->num_rows($result)>0){
		$result = $db->fetch_assoc($result);
		if($result["end"]!=""){
			$uses_for_the_day = $result["start"]-$result["end"]+$result["income"];
			$msg->write(" - ".split_price($uses_for_the_day)."лв.");
			$month_data[]=$uses_for_the_day;
		}
	} else {
		$month_data[] = 0;
	}
	
	$msg->write(" </td>\n");

	$day_count++;
	
	if($day_count>7){
		$msg->write("</tr>\n<tr>\n");
		$day_count =1;
	}
}

while($day_count>1 && $day_count <=7){
	$msg->write("<td> </td>");
	$day_count++;
}
$msg->write("</tr></table>");

$query = $db->query("SELECT * FROM uses JOIN products ON uses.product_id = products.pid where uses.date >= '{$date->curr_year}-{$date->curr_month}-01' AND uses.date <= '{$date->curr_year}-{$date->curr_month}-$days_in_month'");
$sum= 0;
if($db->num_rows($query) >0){
	while($row = $db->fetch_assoc($query)){
		//echo $row["product"].":".split_price($row["price"])." лв. <br />";
		$sum +=  $row["price"];
	}
	$msg->info("Разходи за месеца: <strong>".split_price($sum)."лв.</strong>");
}

$query = $db->query("select * from balances where date >= '{$date->curr_year}-{$date->curr_month}-01' AND date <= '{$date->curr_year}-{$date->curr_month}-$days_in_month' AND income > 0");
if($db->num_rows($query) >0){
	$sum =0;
	$msg->write("<div id=\"list\">");
	$msg->write("<span>Приходи за месецa :</span>\n<ul>");
	while($row = $db->fetch_assoc($query)){
		$msg->write("<li>".$row["date"]." : ".split_price($row["income"])." лв. </li>");
		$sum +=  $row["income"];
	}
	$msg->write("</ul>");
	$msg->write("<span>Общо приходи: ".split_price($sum).'лв.</span>');
	$msg->write("</div>");
}

$data ="";
for($i =0;$i<$days_in_month;$i++){
	$data.=($i."=".split_price($month_data[$i])."&amp;");
}


$msg->write('<br /><img src="graph_image.php?'.$data.'" />');
?>